<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/4/2
 * Time: 9:58
 */
namespace app\api\controller;
use think\Db;
class  Luck extends Base {
      /**
       * 异地登录验证
       * @return string
       */
//    public function __construct()
//    {
//        extract(input());
//        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
//        if($auth == 112){
//            echo json_encode(['status'=>301,'msg'=>$this->otherLogin]);
//            exit;
//        }elseif($auth == 110){
//            echo json_encode(['status' => 302, 'msg' => $this->frost]);
//            exit;
//        }
//    }
      public function index(){
          extract(input());
//          $user_id = 1;
          //奖池总共有多少金币
          $coin = Db::table('up_other')->where('other_id', 9)->value('other_price');
          //查询这个用户有多少金币
          $price = Db::table('up_u_user')->where('user_id', $user_id)->value('user_balance');
          //查询中奖用户
          $res = Db::table('up_lucky_results')->alias('a')
                    ->join('up_u_user uuu','uuu.user_id = a.user_id','left')
                    ->join('up_lucky ul','ul.id = a.id','left')
//                    ->field('uuu.user_username,ul.name,ul.gold')
                    ->field('a.get_gold,uuu.user_username,ul.name')
                    ->order('a.id desc')
                    ->limit(10)
                    ->select();
          $list['coin'] = $coin;
          $list['price'] = $price;
          $list['res'] = $res;
          $list['need'] = 100;
          return  json(array('status'=>200,'msg'=>$list));
      }

      //
      public function luck(){
          $list = Db::table('up_lucky')->select();
          return  json(array('status'=>200,'msg'=>$list));
      }
    /**
     * APP抽奖时就调这个接口，而不是抽完调
     * 每一次抽奖都要调后台，由后台来告诉APP用户中了几等奖，并在同时就发放奖励，APP端只需告诉用户中了什么奖即可
     * @return \think\response\Json
     */
      public function luckMan(){
          extract(input());
          $my = Db::table('up_u_user')->where('user_id', $user_id)->value('user_balance');
          if($my < 100){
              return  json(array('status'=>0,'msg'=>'金币不足'));
          }
          $list = Db::table('up_lucky')->order('gold DESC,odds ASC')->select();
          $first_id = $list[0]['id'];
          $have = Db::table('up_lucky_results')
              ->where('luck_id',$first_id)
              ->whereTime('add_time','w')
              ->find();
          if($have){
              $num = rand($list[0]['odds'] * 100 + 1,10000);
          }else{
              $num = rand(1,10000);
          }
          $res = [];
          $start = 1;
          foreach ($list as $k => $v){
              $end = $v['odds'] * 100 + $start - 1;
              if($k + 1 == count($list)){
                  $end = 10000;
              }
              if($num >= $start && $num <= $end){ //中奖等级
                  $res = $v;
              }
              $start = $end + 1;

          }
          // 启动事务
          Db::startTrans();
          try{
              //2、奖金池先加70，后减用户所得和比例
              Db::table('up_other')->where('other_id', 9)->setInc('other_price',70);
              $coin = Db::table('up_other')->where('other_id', 9)->value('other_price');
              $two_get = ($coin - $res['gold']) * $res['bai'] / 100;
              //-----------------特殊情况 start --------------
              if($two_get < 0){
                  $two_get = 0;
              }
              //-----------------特殊情况  end  --------------
              $get_and_less = (int)$two_get + $res['gold'];
              Db::table('up_other')->where('other_id', 9)->setDec('other_price',$get_and_less);
              //1、用户减金币，用户加抽奖得到的金币和比例
              Db::table('up_u_user')->where('user_id', $user_id)->setDec('user_balance',100);
              Db::table('up_u_user')->where('user_id', $user_id)->setInc('user_balance',$get_and_less);

              //-----------------特殊情况 start --------------
              if($coin < $get_and_less){
                  Db::table('up_other')->where('other_id', 9)->setField('other_price',0);
              }
              //-----------------特殊情况  end  --------------
              //记录入库
              $insert = [
                  'user_id' => $user_id,
                  'luck_id' => $res['id'],
                  'get_gold' => $get_and_less,
                  'add_time' => date('Y-m-d H:i:s'),
              ];
              Db::table('up_lucky_results')->insert($insert);
              Db::commit();
              return json(['status'=>200,'msg'=>$res]);
          } catch (\Exception $e) {
              // 回滚事务
              Db::rollback();
              return json(['status'=>100,'msg'=>'数据错误，请刷新重试']);
          }
      }
  }